package com.rt.sms.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @classname: 短信发送
 * @author：
 * @time： 2017/5/26
 */
public class SendMessageUtil {

    //"http://ppyw.paopao106.cn:7862/sms?action=send&account=922170&password=nsBXhC&mobile=15106929160&content=&extno=10690170&rt=json";
    //private static final String url = "http://ppyw.paopao106.cn:7862/sms";
    private static final String url = "http://wang.paopao106.cn:7862/sms";
    private static final String action = "send";
    private static final String account = "922170";
    private static final String password = "nsBXhC";
    private static final String extno = "10690170";
    private static final String rt = "json";


    public static String postUrl(String content, String mobile) {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("action", action));
        params.add(new BasicNameValuePair("account", account));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("mobile", mobile));
        params.add(new BasicNameValuePair("content", content));
        params.add(new BasicNameValuePair("extno", extno));
        params.add(new BasicNameValuePair("rt", rt));

        RTLogger.getLogger("SMSCenterLog").debug("接收者手机号："+mobile+"；短信内容："+content);

        try {
            httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 设置参数给Post
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if (!response.getStatusLine().getReasonPhrase().equals("OK")) {
                //return "发送交易失败!";
                return null;
            }
            //读取返回信息
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    entity.getContent(), "UTF-8"));
            StringBuffer buffer1 = new StringBuffer();
            String line = reader.readLine();

            while (line != null) {
                buffer1.append(line);
                line = reader.readLine();
            }
            RTLogger.getLogger("SMSCenterLog").debug("短信平台返回信息："+buffer1.toString());
            return buffer1.toString();

        } catch (Exception e) {

        }
        return null;
    }


    public static String postPramaList(List<NameValuePair> list, String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        InputStreamReader streamReader = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); // 设置参数给Post
            response = httpClient.execute(httpPost);
            if (!response.getStatusLine().getReasonPhrase().equals("OK")) {
                //return "发送交易失败!";
                return "";
            }
            HttpEntity entity = response.getEntity();
            streamReader = new InputStreamReader(entity.getContent(), "UTF-8");
            //读取返回信息
            BufferedReader reader = new BufferedReader(streamReader);
            StringBuffer buffer = new StringBuffer();
            String line = reader.readLine();
            while (line != null) {
                buffer.append(line);
                line = reader.readLine();
            }
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (streamReader != null) {
                    streamReader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (response != null) {
                    response.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static void main(String[] args){
        postUrl("【融托民登】您的报名验证码123456，5分钟内有效。如非本人操作请忽略本短信。","15106929160");
    }

}
